package com.hnqcgczy.controller;

import com.hnqcgczy.service.IShopCartService;
import com.hnqcgczy.service.impl.ShopCartServiceImpl;
import com.hnqcgczy.vo.TShopcart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addcart")
public class AddShopCartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户登陆状态
        String tuId=(String)req.getSession().getAttribute("tuId");
        //判断如果tuId为null，跳转登陆
        if(tuId==null){
            resp.sendRedirect("/login.jsp");
            return;
        }

        //获取商品编号
        String tpId=req.getParameter("tpId");
        //创建购物车对象
        TShopcart cart=new TShopcart();
        cart.setTuId(tuId);
        cart.setTpId(Long.parseLong(tpId));
        cart.setTsCount(1);

        //调用服务层 完成新增
        IShopCartService cartService=new ShopCartServiceImpl();
        cartService.addShopCart(cart);

        //跳转到我的购物车页
        resp.sendRedirect("/shopCart");
    }
}
